/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.schedule.dynamic.web;
import com.google.inject.Inject;
import com.hyts.mvc.annotation.MvcMapping;
import com.hyts.schedule.dynamic.bean.TaskJob;
import com.hyts.schedule.dynamic.serv.impl.MemoryTaskJobService;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;

import java.util.List;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.schedule.dynamic.web
 * @author:LiBo/Alex
 * @create-date:2020-04-06 14:30
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */

@MvcMapping("/task")
public class TaskJobController {

    @Inject
    MemoryTaskJobService taskJobService;


    @MvcMapping("/runs")
    public List<TaskJob> runs(){
        return taskJobService.getRunningTasks(null);
    }

    @MvcMapping("/plans")
    public List<TaskJob> plans(){
        return taskJobService.getPlanTasks(null);
    }


    @MvcMapping("/pause")
    public String pause(){
        TaskJob taskJob = new TaskJob();
        taskJob.setJobId("123123");
        taskJob.setJobName("ceshi");
        taskJob.setJobStatus("1");
        taskJob.setJobGroup("asda");
        taskJob.setCronExpression("* * * * * ?");
        taskJobService.pauseTask(taskJob);
        return "pause";
    }


    @MvcMapping("/resume")
    public String resume(){
        return "131";
    }


    @MvcMapping("/remove")
    public String remove(){
        return "131";
    }


    @MvcMapping("/run")
    public String run(){
        return "131";
    }


    @MvcMapping("/update")
    public String update(){
        return "131";
    }


    @MvcMapping("/add")
    public String add(){
        TaskJob taskJob = new TaskJob();
        taskJob.setJobId("123123");
        taskJob.setJobName("ceshi");
        taskJob.setJobStatus("1");
        taskJob.setJobGroup("asda");
        taskJob.setCronExpression("* * * * * ?");
        Scheduler scheduler = taskJobService.addTask(taskJob);
        try {
            if(!scheduler.isStarted()){
                scheduler.start();
            }
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return "success";
    }

    public static void main(String[] args){
        new TaskJobController().add();
    }

}
